import axios from "../api/index";
// 提交
const submit = (data) => {
  return axios.request({
    url: "/flow/runtime/submit",
    method: "post",
    data
  });
};
// 获取下一步操作人
// const getNextUsers = (data) => {
//   return axios.request({
//     url: "/flow/runtime/nextTaskJoinsPage",
//     method: "post",
//     data
//   });
// };
// 通过流程id和taskId获取下一步操作人
const getNextUsersById = (data) => {
  return axios.request({
    url: "/system/roleAccount/getIdByRoles",
    method: "post",
    data: data
  });
};
/**
  * 提交And审核
  * @param data 工作流所需参数
  * @param callback 成功回调函数
  * @param text 提示语字符串
  * @param isGetUsers 是否需要查询下一步操作人
  */
export const flowSubmit = async (data, successCallBack, text, isGetUsers = false, isSubmit = true) => {
  const { bizKey, flowKey, taskId, callbackUrl, idea = "1", ideaMsg = "", variable = {} } = data;
  const params1 = {
    bizKey,
    flowKey,
    idea,
    ideaSecond: "",
    notNeedUserId: [],
    orgId: "",
    pageNum: 1,
    pageSize: 100,
    rygw: "",
    rymc: "",
    taskId,
    userId: ""
  };
  let nextAssigneeList = [];
  if (isGetUsers) {
    const nextUserData = await getNextUsersById(params1);
    // const nextUserData = await getNextUsers(params1);
    if (nextUserData.data.length === 0) {
      return this.$message({ message: "下一步操作人获取失败！", type: "error" });
    }
    nextAssigneeList = nextUserData.data.map(el => el.iscId);
  }
  const params2 = {
    assignee: JSON.parse(sessionStorage.getItem("account")).id,
    bizKey,
    callbackId: 0,
    callbackUrl,
    flowKey,
    idea,
    ideaMsg,
    ideaSecond: "",
    ideaSuggest: "",
    nextAssigneeList,
    nextTaskId: "",
    nextTaskName: "",
    operationType: 0,
    taskId,
    variable
  };
  const res = await submit(params2);
  if (res) {
    successCallBack();
  } else {
    this.$message({
      message: `${text}失败`,
      type: "error"
    });
  }
};
export const handleGetNextUsers = async (data) => {
  console.log(data);
  const { bizKey, flowKey, taskId, idea = "1", ideaMsg } = data;
  const params1 = {
    bizKey,
    flowKey,
    idea,
    ideaMsg,
    ideaSecond: "",
    notNeedUserId: [],
    orgId: "",
    pageNum: 1,
    pageSize: 100,
    rygw: "",
    rymc: "",
    taskId,
    userId: ""
  };
  const nextUserData = await getNextUsersById(params1);
  if (nextUserData.data.length === 0) {
    return this.$message({ message: "下一步操作人获取失败！", type: "error" });
  }
  return nextUserData.data.map(el => el.iscId);
};
